package com.bstek.jpa.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.transaction.JdbcTransactionManager;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Title: JPAConfig
 * @Package: com.bstek.jpa.config
 * @Description:
 * @Author: zxq
 * @Date: 2023/12/25 - 16:20
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "jpaEntityManagerFactory",
        transactionManagerRef = "jpaTransactionManager",
        basePackages = {"com.bstek.*.dao"}//设置repository所在位置
)
public class JpaConfig {

//    @Value("spring.datasource.url")
//    private String url;
//    @Value("spring.datasource.username")
//    private String username;
//    @Value("spring.datasource.password")
//    private String password;
//    @Value("spring.datasource.driver-class-name")
//    private String driverClassName;


    @Autowired
    private DataSource dataSourceOne;

    @Bean(name = "jpaEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean jpaEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSourceOne);
        //这个扫描的是Entity(JavaBean)的位置，
        em.setPackagesToScan(new String[]{"com.bstek.*.domain"});
        HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(jpaAdapter);
        em.setJpaProperties(jpaProperties());

        return em;
    }

    @Primary
    @Bean(name = "jpaEntityManager")
    public EntityManager jpaEntityManager(EntityManagerFactory jpaEntityManagerFactory) {
        return jpaEntityManagerFactory.createEntityManager();
    }

    @Bean(name = "jpaTransactionManager")
    @Primary
    public PlatformTransactionManager jpaTransactionManager(EntityManagerFactory jpaEntityManagerFactory) {
        JpaTransactionManager jtm = new JpaTransactionManager();
        jtm.setEntityManagerFactory(jpaEntityManagerFactory);
        return jtm;
    }


    @Bean
    public Properties jpaProperties() {
        Properties properties = new Properties();
//        properties.put("hibernate.show_sql","true");
//        properties.put("hibernate.connection.userUnicode","true");
//        properties.put("hibernate.connection.characterEncoding","UTF-8");
//        properties.put("hibernate.format_sql","true");
//        properties.put("hibernate.use_sql_comments","true");
//        properties.put("hibernate.hbm2ddl.auto","update");

        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        return properties;
    }



//    @Bean
//    public DataSource dds() {
//        DruidDataSource dds = new DruidDataSource();
//        dds.setDriverClassName(driverClassName);
//        dds.setUrl(url);
//        dds.setUsername(username);
//        dds.setPassword(password);
//        return dds;
//    }

//    @Bean
//    public DataSource dataSource() {
//        BasicDataSource dataSource = new BasicDataSource();
//        dataSource.setDriverClassName(driverClassName);
//        dataSource.setUrl(url);
//        dataSource.setUsername(username);
//        dataSource.setPassword(password);
//        return dataSource;
//    }


}
